package com.iqiyi.pps.epg.core.service.auth;

import com.iqiyi.pps.epg.core.dao.auth.RoleDao;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.auth.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统角色业务类
 */
@Service
@Transactional
public class SysRoleManager {

	private static Logger logger = LoggerFactory.getLogger(SysRoleManager.class);

    @Autowired
	private RoleDao roleDao;

    /**
	 * 检查用角色是否唯一.
     * @param newRoleName  新的角色名
     * @param oldRoleName  旧的角色名
	 * @return RoleName在数据库中唯一或等于oldRoleName时返回true.
	 */
	@Transactional(readOnly = true)
	public boolean isRoleNameUnique(String newRoleName, String oldRoleName) {
		return roleDao.isPropertyUnique("name", newRoleName, oldRoleName);
	}

    /**
     * 根据主键取角色对象
     * @param id 主键ID
     * @return Role
     */
	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

    /**
     * ID升序的方式取出所有角色
     * @return List<Role>
     */
	@Transactional(readOnly = true)
	public List<Role> getAllRole() {
		return roleDao.getAll("id", true);
	}

    /**
     * 使用属性过滤条件查询角色
     * @param page Page<Role>
     * @param filters 过滤条件
     * @return Page<Role>
     */
    @Transactional(readOnly = true)
    public Page<Role> searchRole(final Page<Role> page, final List<PropertyFilter> filters) {
        return roleDao.findPage(page, filters);
    }

    /**
     * 保存角色对象
     * @param entity 角色对象
     */
	public void saveRole(Role entity) {
		roleDao.save(entity);
        System.out.println("saveRole");
	}

    /**
     * 根据主键ID删除角色
     * @param id  主键ID
     */
	public void deleteRole(Long id) {
		roleDao.delete(id);
	}
}
